/**拿数据连接*/
package com.jinxinol.core.util.autosrc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class GetConnection {
	private static Properties env;
	public GetConnection(){}
//	static{                       //读取配置文件信息
//		try{
//			env = new Properties();
//			InputStream  is = getConnection.class.getResourceAsStream("/chvpq/config/GetConn.properties");
//			env.load(is);
//			is.close();
//		}catch(Exception e){
//			e.printStackTrace();
//			throw new ExceptionInInitializerError( e ) ;
//		}
//	}
	
	//线程安全
	public static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
	
	/**
	 * 获得数据库连接(Connection)方法 连接池
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnJndi()throws Exception{
		Connection conn = tl.get();
		if(conn == null){
			Context ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup(env.getProperty("jndi_name"));
			conn = ds.getConnection();
			tl.set(conn);
		}
		return conn;
	}
	
	/**
	 * 获得数据库连接(Connection)方法 我的电脑
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnJdbc_161()throws Exception{
		Connection conn = tl.get();
		if(conn == null){
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url = "jdbc:oracle:thin:@192.168.30.161:1521:xe";
			String uname = "jxis2";
			String upwd = "jxis2";
			conn = DriverManager.getConnection(url,uname,upwd);
			tl.set(conn);
		}
		return conn;
	}
	
	/**
	 * 释放资源
	 */
	public static void release(Statement pstm, ResultSet rs , Connection conn){
       if( pstm != null ) try{ pstm.close();}catch( Exception e ){e.printStackTrace();}
       if( rs != null ) try{ rs.close(); }catch( Exception e ){e.printStackTrace();}
       if( conn != null ) try{ conn.close(); tl.remove(); }catch(Exception e ){ e.printStackTrace(); } 
    }
	
	/**
	 * 开启事务（conn.setAutoCommit(false)）
	 * @param conn
	 */
	public static void beginTransaction(Connection conn) {
		try {
			if (conn != null) {
				if (conn.getAutoCommit()) {
					conn.setAutoCommit(false); //手动提交
				}
			}
		}catch(SQLException e) {}
	}
	
	/**
	 * 提交事务（conn.commit();）
	 * @param conn
	 */
	public static void commitTransaction(Connection conn) {
		try {
			if (conn != null) {
				if (!conn.getAutoCommit()) {
					conn.commit();
				}
			}
		}catch(SQLException e) {}
	}
	/**
	 * 事务回滚
	 * @param conn
	 */
	public static void rollbackTransaction(Connection conn) {
		try {
			if (conn != null) {
				if (!conn.getAutoCommit()) {
					conn.rollback();
				}
			}
		}catch(SQLException e) {}
	}
	
	/**
	 * 系统调用的链接方法
	 * @return
	 * @throws Exception
	 */
	public static Connection getConn()throws Exception{
		return getConnJdbc_161();
	}
	
}
